package cn.gov.anhai.service;

import cn.gov.anhai.mappers.PublicMapper;
import cn.gov.anhai.mappers.RuralCulturalTourismMapper;
import cn.gov.anhai.mappers.TourismMapper;
import cn.gov.anhai.utils.ApiResult;
import cn.gov.anhai.utils.LoginUtil;
import cn.gov.anhai.utils.MyUtil;
import cn.gov.anhai.utils.PageUtil;
import cn.gov.anhai.vo.OssVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 旅游动态
 */
@Service
public class TourismService {
    @Resource
    PublicMapper publicMapper;

    @Resource
    PublicService publicService;

    @Resource
    TourismMapper tourismMapper;

    /**
     * 旅游动态列表
     */
    public List<Map<String, Object>> list(Integer current, Integer size, Long type, String name, Long userId, String hitStart, String hitEnd) {
        PageUtil pageUtil = new PageUtil(current, size);
        List<Map<String, Object>> list = tourismMapper.list(pageUtil.getOffset(), pageUtil.getRow(), type, name, userId, hitStart, hitEnd);
        return list;
    }

    /**
     * 旅游动态列表（个数）
     */
    public Integer listCount(Long type, String name, Long userId, String hitStart, String hitEnd) {
        Integer count = tourismMapper.listCount(type, name, userId, hitStart, hitEnd);
        return count;
    }

    /**
     * 根据编号查询乡信息
     */
    public Map<String, Object> info(Long id, Long uid) {
        Map<String, Object> info = tourismMapper.info(id);
        if (info == null) return null;
        tourismMapper.updateCount(id);

        Map<String, Object> result = new HashMap<>();
        result.put("id", info.get("id"));//
        result.put("title", info.get("name"));//标题
        result.put("vr", info.get("vr_link"));//vr地址
        result.put("intro", publicService.getOSSUrl((Long) info.get("voice_intro_oss")));//语音简介

        //轮播信息
        List<String> swiper = new ArrayList<>();
        MyUtil.addItem(swiper, publicService.getOSSUrl((Long) info.get("intro_video_oss")));
        MyUtil.addItem(swiper, publicService.getOSSUrl((String) info.get("intro_image_oss")));
        if (swiper.size() == 0)
            MyUtil.addItem(swiper, publicService.getOSSUrl((Long) info.get("cover")));
        result.put("swiper", swiper);

        result.put("content", info.get("intro"));//
        result.put("view", info.get("punch_card_count"));//浏览次数

        //地址信息
        Map<String, Object> pos = new HashMap<>();
        pos.put("lat", info.get("latitude"));//纬度
        pos.put("lng", info.get("longitude"));//经度
        result.put("pos", pos);

        result.put("phones", MyUtil.getString((String) info.get("phone")).split("\\|"));//手机号码数组
        result.put("address", info.get("address"));//地址
        result.put("openTime", info.get("open_time"));//开放时间

        result.put("isAppointment",info.get("is_appointment"));//是否需要预约
        result.put("ticket", info.get("ticket"));//门票

        //是否收藏
        if (uid == null) {
            result.put("isCollect", false);
        } else {
            result.put("isCollect", publicService.isUserDynamic(uid, 3, (Long) info.get("id")));
        }

        return result;
    }

    /**
     * 所有标记类型
     */
    public List<Map<String, Object>> makerType() {
        List<Map<String, Object>> list = tourismMapper.makerType();
        list.forEach(item -> {
            item.put("icon", publicService.getOSSUrl((Long) item.get("icon")));
            item.put("selectedIcon", publicService.getOSSUrl((Long) item.get("selectedIcon")));
            item.put("color", item.get("color"));
            item.put("selectBackColor", item.get("selectBackColor"));
        });
        return list;
    }

    /**
     * 所有标记
     */
    public List<Map<String, Object>> maker() {
        List<Map<String, Object>> list = tourismMapper.maker();
        list.forEach(item -> {
            OssVO cover = publicService.getOssInfo((Long) item.get("cover"));
            item.put("cover", cover.getUrl());
            item.put("previewUrl", cover.getPreviewUrl());
            item.put("icon", publicService.getOSSUrl((Long) item.get("icon")));
            item.put("zoom",item.get("zoom"));
        });
        return list;
    }

    /**
     * 收藏文旅信息
     */
    public Map<String, Object> collect(Long uid, Long id) {
        return publicService.doUserDynamic(uid, 3, id, "isCollect");
    }

}
